package cn.hanyx.vlog.points.controller;


import cn.hanyx.vlog.api.dto.ResponseObject;
import cn.hanyx.vlog.api.vo.Order;
import cn.hanyx.vlog.api.vo.Points;
import cn.hanyx.vlog.points.service.IPointsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hanyx
 * @since 2021-11-27
 */
@RestController
@RequestMapping("/points")
public class PointsController {

    @Autowired
    private IPointsService pointsService;

    @PutMapping
    public ResponseObject update(@RequestBody Points points) {
        return new ResponseObject(pointsService.updateById(points));
    }

    @PutMapping("/order")
    public ResponseObject updateByOrder(@RequestBody Order order) {
        if (order.getUsername() == null) {
            throw new RuntimeException("order is empty");
        }

        QueryWrapper<Points> wrapper = new QueryWrapper();
        wrapper.eq("username", order.getUsername());
        Points points = pointsService.getOne(wrapper);
        //        // 积分计算
        Integer quantity = points.getQuantity();
        quantity = quantity + order.getAmount().intValue() / 100;
        points.setQuantity(quantity);
        return new ResponseObject(pointsService.updateById(points));
    }
}

